<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 13] 13.3 Operators</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 16:00:51 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 13<br>Java Syntax</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch13_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-13-SECT-3">13.3 Operators</A></h2>

<P CLASS=para>
<A NAME="CH13.OPERATORS-LI1"></A><A NAME="CH13.PRECEDENCE.1"></A><A HREF="ch13_03.htm#JNUT2-CH-13-SECT-3">Operators</A>
lists the operators of the Java language, along with their precedence,
operand types, and associativity.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JNUT2-CH-13-TAB-3">Table 13.3: Java Operators</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">Prec.</TH>
<TH ALIGN="left">Operator</TH>
<TH ALIGN="left">Operand Type(s)</TH>
<TH ALIGN="left">Assoc.</TH>
<TH ALIGN="left">Operation Performed</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">1</TD>
<TD ALIGN="left">++</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">pre-or-post increment (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">--</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">pre-or-post decrement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+, -</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">unary plus, unary minus</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">~</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">bitwise complement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">logical complement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">(<I CLASS=emphasis>type</I>)</TD>
<TD ALIGN="left">any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">cast</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">2</TD>
<TD ALIGN="left">*, /, %</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">multiplication, division, remainder</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">3</TD>
<TD ALIGN="left">+, -</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">addition, subtraction</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+</TD>
<TD ALIGN="left">string</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">string concatenation</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">4</TD>
<TD ALIGN="left">&lt;&lt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">left shift</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;&gt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">right shift with sign extension</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;&gt;&gt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">right shift with zero extension</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">5</TD>
<TD ALIGN="left">&lt;, &lt;=</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">less than, less than or equal</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;, &gt;=</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">greater than, greater than or equal</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">instanceof</TD>
<TD ALIGN="left">object, type</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">type comparison</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">6</TD>
<TD ALIGN="left">==</TD>
<TD ALIGN="left">primitive</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">equal (have identical values)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!=</TD>
<TD ALIGN="left">primitive</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">not equal (have different values)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">==</TD>
<TD ALIGN="left">object</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">equal (refer to same object)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!=</TD>
<TD ALIGN="left">object</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">not equal (refer to different objects)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">7</TD>
<TD ALIGN="left">&amp;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&amp;</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">8</TD>
<TD ALIGN="left">^</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise XOR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">^</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean XOR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">9</TD>
<TD ALIGN="left">|</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">|</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">10</TD>
<TD ALIGN="left">&amp;&amp;</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">conditional AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">11</TD>
<TD ALIGN="left">||</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">conditional OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">12</TD>
<TD ALIGN="left">?:</TD>
<TD ALIGN="left">boolean, any, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">conditional (ternary) operator</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">13</TD>
<TD ALIGN="left">

<P CLASS=para>
=</TD>
<TD ALIGN="left">variable, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">assignment</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">

<P CLASS=para>
*=, /=, %=, +=, -=, &lt;&lt;=, &gt;&gt;=, &gt;&gt;&gt;=, &amp;=, ^=, |=</TD>
<TD ALIGN="left">variable, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">assignment with operation</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
Operator precedence controls the order in which operations are
performed.  Consider the following example:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
w = x + y * z;
</PRE>
</DIV>

<P CLASS=para>
The multiplication operator <tt CLASS=literal>*</tt> has a higher
precedence than the addition operator <tt CLASS=literal>+</tt>, so the
multiplication is performed before the addition.
Furthermore, the assignment operator <tt CLASS=literal>=</tt> has the
lowest precedence of any operator, so the assignment is
done after all the operations on the right-hand side are
performed.  Operators with the same precedence (like
addition and subtraction) are performed in order according
to their associativity
(usually left-to-right).  Operator precedence
can be overridden with the explicit use of parentheses. For example:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
w = (x + y) * z;
</PRE>
</DIV>

<P CLASS=para>
The associativity of an operator specifies the order that
operations of the same precedence are performed in.  In
<A HREF="ch13_03.htm#JNUT2-CH-13-TAB-3">Table 13.3</A>
a value of L specifies left-to-right associativity, and a
value of R specifies right-to-left associativity.
Left-to-right associativity means that operations are
performed left-to-right.  For example:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
w = x + y + z;
</PRE>
</DIV>

<P CLASS=para>
is the same as:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
w = ((x + y) + z);
</PRE>
</DIV>

<P CLASS=para>
because the addition operator has left-to-right
associativity. On the other hand, the following expressions:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
x = ~-~y;
q = a?b:c?d:e?f:g;
</PRE>
</DIV>

<P CLASS=para>
are equivalent to:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
x = ~(-(~y));
q = a?b:(c?d:(e?f:g));
</PRE>
</DIV>

<P CLASS=para>
because the unary operators and the ternary conditional
<tt CLASS=literal>?:</tt> operator have right-to-left associativity.

<P CLASS=para>
Java operators are basically identical to C operators,
except for these differences:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>The <tt CLASS=literal>+</tt> operator applied to <tt CLASS=literal>String</tt> values
concatenates them.  If only one operand of <tt CLASS=literal>+</tt> is a
<tt CLASS=literal>String</tt>, the other one is converted to a string.
The conversion is done automatically for primitive types
and by calling the <tt CLASS=literal>toString</tt> method of non-primitive
types.

<P>
<li CLASS=listitem>Java does not have the comma operator like C does.  It does,
however, simulate this operator in the limited context of the
<tt CLASS=literal>for</tt> loop initialization and increment expressions.

<P>
<li CLASS=listitem>Since all Java integral types are signed, the <tt CLASS=literal>&gt;&gt;</tt>
operator always does a signed right shift, filling in high
bits with the sign bit of the operand.  The new <tt CLASS=literal>&gt;&gt;&gt;</tt>
operator performs an unsigned right shift, filling in high
bits of the shifted value with zero bits.

<P>
<li CLASS=listitem>The <tt CLASS=literal>&amp;</tt> and <tt CLASS=literal>|</tt> operators perform bitwise AND
and OR operations on integral operands, and perform logical
AND and OR operators on <tt CLASS=literal>boolean</tt> operands.
<tt CLASS=literal>&amp;&amp;</tt> and <tt CLASS=literal>||</tt> also perform logical AND and OR on
<tt CLASS=literal>boolean</tt> operands, but do not evaluate the right-hand
operand, if the result of the operation is fully determined
by the left-hand operand.

<P>
<li CLASS=listitem>The <tt CLASS=literal>instanceof</tt> operator returns <tt CLASS=literal>true</tt> if the
object on the left-hand side is an instance of the class or
implements the interface on the right-hand side.  Otherwise
it returns <tt CLASS=literal>false</tt>.  If the left-hand side is
<tt CLASS=literal>null</tt>, it returns <tt CLASS=literal>false</tt>.

<P>
</UL>
<P CLASS=para>
</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch13_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Character Escape Sequences</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Modifiers</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
